﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using Resources;
public partial class admin_AddRelatedItem : SitePage
{
    #region ViewState
    public int count = 0;
    public int ProductID
    {

        get
        {

            object o = ViewState["ProductID"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["ProductID"] = value; }

    }
    public int PageIndex
    {

        get
        {

            object o = ViewState["PageIndex"];

            if (o != null) return (int)o;

            return 0;

            //a default

        }

        set { ViewState["PageIndex"] = value; }

    }   
    #endregion

    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.ProductID = clsQueryString.GetInt("id");
            LoadData();
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitData();
    }
    #endregion

    #region Events
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string message = string.Empty;
        if (Page.IsValid)
        {
            string[] arraySKU = txtSKU.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            if (arraySKU.Length > 0)
            {
                busProductRelated objBus = new busProductRelated();
                int resultInsert = 0;
                for (int i = 0; i < arraySKU.Length; i++)
                {
                    if (arraySKU[i].ToString() != string.Empty)
                    {
                        ProductRelated objProduct = new ProductRelated();
                        objProduct.ProductID = this.ProductID;
                        objProduct.SKU = arraySKU[i].ToString();
                        resultInsert = objBus.Insert(objProduct);
                        if (resultInsert <= 0)//insert eror or item is alredy insert or SKU is not exist
                            message += objProduct.SKU + ",";
                    }
                }
                if (message != string.Empty)
                {
                    message = message.Substring(0, message.Length - 1);
                    lblErrorMessage.Text = string.Format(Resources.Resource.MSG_INSERT_RELATEDPRODUCT, message);
                }
                this.PageIndex = 1;
                LoadData();
            }
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductList.aspx?" + this.GetPageQuery);
    }
    protected void Pager2_PageIndexChanging(object obj, PageIndexChangeEventArgs e)
    {
        this.PageIndex = e.PageIndex;
        LoadData();
    }
    protected void Pager2_PageSizeChanging(object obj, PageSizeChangeEventArgs e)
    {
        this.PageIndex = 1;
        Pager2.PageSize = Pager1.PageSize = e.PageSize;
        LoadData();
    }
    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Product objProdjuct = (Product)e.Item.DataItem;
            if (objProdjuct == null)
                return;
            hidProductID.Value += objProdjuct.ProductID.ToString() +";";
            //image button Order
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbOrderDown");
            if (e.Item.ItemIndex + 1 == count)
                imgButton.Visible = false;
            else
            {
                imgButton.ToolTip = Resources.Resource.TIP_MOVEDOWN;
                imgButton.CommandArgument = this.ProductID + Environment.NewLine + objProdjuct.ProductID;
                imgButton.ImageUrl = clsString.ORDERDOWN_IMAGE_URL;
            }
            imgButton = (ImageButton)e.Item.FindControl("imbOrderUp");
            if (e.Item.ItemIndex == 0)
                imgButton.Visible = false;
            else
            {
                imgButton.CommandArgument = this.ProductID + Environment.NewLine + objProdjuct.ProductID;
                imgButton.ToolTip = Resources.Resource.TIP_MOVEUP;
                imgButton.ImageUrl = clsString.ORDERUP_IMAGE_URL; ;
                imgButton.Visible = true;
            }
            //image button IsActive
            imgButton = (ImageButton)e.Item.FindControl("imbActive");            
            if (objProdjuct.IsActive == true)
            {
                imgButton.ImageUrl = clsString.ACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_DEACTIVE;
                imgButton.CommandName = "DeActive";
            }
            else
            {
                imgButton.ImageUrl = clsString.DEACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_ACTIVE;
                imgButton.CommandName = "Active";
            }
            imgButton.Attributes.Add("onClick", "javascript:return false;");
            //image button IsNew
            imgButton = (ImageButton)e.Item.FindControl("imbIsNew");            
            if (objProdjuct.IsNew == true)
            {
                imgButton.ImageUrl = clsString.ACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_REMOVENEW;
                imgButton.CommandName = "RemoveNew";
            }
            else
            {
                imgButton.ImageUrl = clsString.DEACTIVE_IMAGE_URL;
                imgButton.ToolTip = Resources.Resource.TIP_SETNEW;
                imgButton.CommandName = "SetNew";
            }
           // imgButton.Enabled = false;
            imgButton.Attributes.Add("onClick", "javascript:return false;");
            //image button
            imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE + "');");
            imgButton.CommandArgument = this.ProductID + Environment.NewLine + objProdjuct.ProductID;
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
        }

    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string[] arrID = e.CommandArgument.ToString().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        int productId = clsConvert.Int(arrID[0].ToString());
        int relatedProductId = clsConvert.Int(arrID[1].ToString());
        switch (e.CommandName)
        {

            case "Delete":
                {
                    Delete(productId, relatedProductId);
                    break;
                }
            case "OrderUp":
                {
                    OrderUp(productId, relatedProductId);
                    break;
                }
            case "OrderDown":
                {
                    OrderDown(productId, relatedProductId);
                    break;
                }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.PageIndex = 1;
        LoadData();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ProductCollection lstCate = new ProductCollection();
        string[] arr = hidProductIDSelect.Value.Split(';');
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].ToString() != string.Empty)
            {                
                busProductRelated _busProductRelated = new busProductRelated();
                ProductRelated objProduct = new ProductRelated();
                objProduct.ProductID = this.ProductID;
                objProduct.ProductRelatedID = clsConvert.Int(arr[i].ToString());
                _busProductRelated.Delete(objProduct);
            }
        }
        LoadData();
    }
    #endregion

    #region Methods
    private void Delete(int productId, int relatedProductId)
    {
        busProductRelated _busProductRelated = new busProductRelated();
        ProductRelated objProduct = new ProductRelated();
        objProduct.ProductID = productId;
        objProduct.ProductRelatedID = relatedProductId;
        if (productId > 0 && relatedProductId > 0 && _busProductRelated.Delete(objProduct))
            LoadData();
    }
    private void LoadData()
    {
        hidProductID.Value = string.Empty;
        //get Condition
        Product objProduct = new Product();
        objProduct.CurrentPage = this.PageIndex;
        objProduct.PageSize = Pager1.PageSize;
        objProduct.ProductID = this.ProductID;
        busProduct _busProduct = new busProduct();
        ProductCollection lstResult = _busProduct.GetRelatedForProduct(objProduct);
        if (lstResult != null && lstResult.Count > 0)
        {
            count = lstResult.Count;
            rptData.DataSource = lstResult;

            divGrid.Visible = true;
            divEmplty.Visible = false;
        }
        else
        {
            rptData.DataSource = null;
            divGrid.Visible = false;

            divEmplty.Visible = true;
            divEmplty.InnerHtml = Resource.MSG_EMPTY;
        }
        rptData.DataBind();
        Pager1.SetPaging(this.PageIndex, count, objProduct.TotalRecords);
        Pager2.SetPaging(this.PageIndex, count, objProduct.TotalRecords);
    }
    private void InitData()
    {
        this.PageIndex = 1;
        this.Pager2.PageSize = this.Pager1.PageSize = clsConfigData.DEFAULT_ADMIN_PAGE_SIZE;       
        reqSKU.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "SKU");
        reqSKU.ValidationGroup = btnAdd.ValidationGroup = vsumProduct.ValidationGroup = "related";
    }
    private void OrderDown(int productId, int relatedProductId)
    {
        busProductRelated _busProductRelated = new busProductRelated();
        ProductRelated objProduct = new ProductRelated();
        objProduct.ProductID = productId;
        objProduct.ProductRelatedID = relatedProductId;
        if (productId > 0 && relatedProductId > 0 && _busProductRelated.OrderDown(objProduct))
            LoadData();
    }
    private void OrderUp(int productId, int relatedProductId)
    {
        busProductRelated _busProductRelated = new busProductRelated();
        ProductRelated objProduct = new ProductRelated();
        objProduct.ProductID = productId;
        objProduct.ProductRelatedID = relatedProductId;
        if (productId > 0 && relatedProductId > 0 && _busProductRelated.OrderUp(objProduct))
            LoadData();
    }
    #endregion
   
}